<?php
/*
 * Copyright notice
 * 
 * (c) 2009 MH-Dev.de, Martin Hesse <info@mh-dev.de>
 * 
 * This script is part of the MH-Dev.-Board project. The MH-Dev.-Board 
 * is free software; you can redistribute it and/or modify it under the 
 * terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, 
 * or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty 
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU 
 * General Public License along with this program; if not, 
 * see http://www.gnu.org/licenses/.
 */


/**
 * Backend NavigationController
 *
 * @author	Martin Hesse <info@mh-dev.de>
 * @package	MH-Dev.-Board
 */
class Backend_NavigationController extends Zend_Controller_Action
{
	private $registry; // registry
	private $auth; // user-values
	private $settings; // board-settings
	private $baseUrl; // helper
	
	/*
	 * Init class vars and stuff
	 */
	public function init()
	{		
		// registry
		$this->registry = Zend_Registry::getInstance();
		
		// settings
		$this->settings	= $this->registry->get('settings');
		
		// helper
		$baseUrl		= new MHDEV_View_Helper_BaseUrl();
		$this->baseUrl	= $baseUrl->BaseUrl();
		
		// auth
		$auth = Zend_Auth::getInstance();
		$this->view->auth = $auth->hasIdentity();
    	
		if($auth->hasIdentity())
		{
			$this->view->auth_role 			= $auth->getIdentity()->role;
			$this->view->username 			= $auth->getIdentity()->username;
			$this->view->auth_uid			= $auth->getIdentity()->uid;
			$this->view->unread_messages	= $auth->getIdentity()->count_unread_messages;
		}
		else
		{
			$this->view->username 	= $this->registry->get('lang')->_('guest');
		}

		$this->auth = $auth;
    	
		// lang
		$this->view->translate = $this->registry->get('lang');
		
		// include js
		$this->view->headScript()->appendFile($this->baseUrl . '/static/js/jquery-1.3.2.min.js');
		$this->view->headScript()->appendFile($this->baseUrl . '/static/js/backend.js');
	}
	
	
	
	/*
	 * empty yet
	 */
	public function indexAction()
	{
		
	}
	
	
	
	/*
	 * Get frontend navigation elements
	 * 
	 * @param null
	 * 
	 * @return void
	 */
	public function frontendAction()
	{
		// post
		$postData	= $this->_request->getPost();
		
		// db
		$navigation	= new FrontendNavigation();
		
		// form
		$form		= new FrontendNavigationForm();
		
		// query
		$getNavi	= $navigation->fetchAll(null, 'sort_id ASC');
		
		// helper
		$helperUrl	= new Zend_View_Helper_Url();
		
		// view
		$this->view->form	= $form;
		$this->view->count	= $getNavi->count();
		$this->view->result	= $getNavi->toArray();
		
		if($this->_request->isPost())
		{
			if($form->isValid($postData))
			{
				$url = $helperUrl->url(array('module' => 'backend', 'action' => 'frontend', 'controller' => 'navigation'), 'be_navi_fe', true);
				$form->removeElement('submit');
				$navigation->insert($form->getValues());
				$this->_redirect('/backend/navigation_fe.html');
			}
			else
			{
				$form->populate($postData);
			}
		}
	}
	
	
	
	/*
	 * handle ajax request for frontend navigation
	 * 
	 * @param null
	 * 
	 * @return void
	 */
	public function requestfrontendAction()
	{
		// disable layout
		Zend_Layout::getMvcInstance()->disableLayout();
		
		// post
		$postData	= $this->_request->getPost();
		
		// db
		$navigation	= new FrontendNavigation();
		
		// helper
		$view		= new Zend_View();
		
		if($this->_request->isPost())
		{
			foreach($postData AS $postVar => $value)
			{
				if($postVar != 'uid')
				{
					if(is_int($value))
					{
						$clean_value	= int_val($value);
					}
					elseif(is_string($value))
					{
						$clean_value	= $view->escape($value);
					}
					$navigation->update(array($postVar => $clean_value), 'uid=' . $postData['uid']);
				}
			}
		}
	}

	
	
	/*
	 * delete a frontend navigation element
	 * 
	 * @param null
	 * 
	 * @return void
	 */
	public function deletefrontendAction()
	{
		// disable layout
		Zend_Layout::getMvcInstance()->disableLayout();
		
		// post
		$postData	= $this->_request->getPost();
		
		// db
		$navigation	= new FrontendNavigation();
		
		if($postData['uid'] > 0)
		{
			$navigation->delete('uid=' . $postData['uid']);
		}
	}
	
	
	
	/*
	 * Get backend navigation elements
	 * 
	 * @param null
	 * 
	 * @return void
	 */
	public function backendAction()
	{
		// post
		$postData	= $this->_request->getPost();
		
		// db
		$navigation	= new BackendNavigation();
		
		// form
		$form		= new BackendNavigationForm();
		
		// query
		$getNavi	= $navigation->fetchAll(null, 'sort_id ASC');
		
		// helper
		$helperUrl	= new Zend_View_Helper_Url();
		
		// view
		$this->view->form	= $form;
		$this->view->count	= $getNavi->count();
		$this->view->result	= $getNavi->toArray();
		
		if($this->_request->isPost())
		{
			if($form->isValid($postData))
			{
				$url = $helperUrl->url(array('module' => 'backend', 'action' => 'backend', 'controller' => 'navigation'), 'be_navi', true);
				$form->removeElement('submit');
				$navigation->insert($form->getValues());
				$this->_redirect('/backend/navigation.html');
			}
			else
			{
				$form->populate($postData);
			}
		}
	}
	
	
	
	/*
	 * handle ajax request for backend navigation
	 * 
	 * @param null
	 * 
	 * @return void
	 */
	public function requestbackendAction()
	{
		// disable layout
		Zend_Layout::getMvcInstance()->disableLayout();
		
		// post
		$postData	= $this->_request->getPost();
		
		// db
		$navigation	= new BackendNavigation();
		
		// helper
		$view		= new Zend_View();
		
		if($this->_request->isPost())
		{
			foreach($postData AS $postVar => $value)
			{
				if($postVar != 'uid')
				{
					if(is_int($value))
					{
						$clean_value	= int_val($value);
					}
					elseif(is_string($value))
					{
						$clean_value	= $view->escape($value);
					}
					$navigation->update(array($postVar => $clean_value), 'uid=' . $postData['uid']);
				}
			}
		}
	}

	
	
	/*
	 * delete a backend navigation element
	 * 
	 * @param null
	 * 
	 * @return void
	 */
	public function deletebackendAction()
	{
		// disable layout
		Zend_Layout::getMvcInstance()->disableLayout();
		
		// post
		$postData	= $this->_request->getPost();
		
		// db
		$navigation	= new BackendNavigation();
		
		if($postData['uid'] > 0)
		{
			$navigation->delete('uid=' . $postData['uid']);
		}
	}
	
	
	
	/*
	 * Change spec. mode of a element
	 * 
	 * @param null
	 * 
	 * @return void
	 */
	public function modeAction()
	{
		// params
		$type	= $this->_request->getParam('type');
		$mode	= $this->_request->getParam('mode');
		$uid	= $this->_request->getParam('uid');
		
		//db
		if($type == 'fe')
		{
			$navigation = new FrontendNavigation();
			$return		= 'backend/navigation_fe.html';
		}
		elseif ($type == 'be')
		{
			$navigation = new BackendNavigation();
			$return		= 'backend/navigation.html';
		}
		
		// change mode
		if(isset($navigation) && $uid > 0)
		{
			if($mode == 'hide')
			{
				$navigation->update(array('hidden' => 1),'uid=' . $uid);
			}
			elseif ($mode == 'unhide')
			{
				$navigation->update(array('hidden' => 0),'uid=' . $uid);
			}
			elseif ($mode == 'lock')
			{
				$navigation->update(array('locked' => 1),'uid=' . $uid);
			}
			elseif ($mode == 'unlock')
			{
				$navigation->update(array('locked' => 0),'uid=' . $uid);
			}
		}
		
		$this->_redirect($return);
	}
}
?>